/*
 * @Descripttion: 啦啦啦哈哈哈
 * @version:
 * @Author: lsy
 * @Date: 2021-08-30 17:16:55
 * @LastEditors: liushengyan
 * @LastEditTime: 2023-03-16 14:45:59
 */
import { defineConfig, loadEnv } from "vite";
import path from "path";
import vue from "@vitejs/plugin-vue";

// elementplus自动导入
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
// 自动导入样式
import ElementPlus from "unplugin-element-plus/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// elementplus icon自动导入
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import Inspect from "vite-plugin-inspect";

import { FileSystemIconLoader } from "unplugin-icons/loaders";

const pathSrc = path.resolve(__dirname, "src");

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  //检查process.cwd()路径下.env.development.local、.env.development、.env.local、.env这四个环境文件
  loadEnv(mode, process.cwd());
  //返回vite配置
  return {
    plugins: [
      vue(),

      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ["vue", "vue-router", "vuex"],
        resolvers: [
          // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
          ElementPlusResolver(),
        ],
        dts: path.resolve(pathSrc, "auto-imports.d.ts"),
      }),

      // 自动导入 Element Plus 相关函数组件的配套样式，如：ElMessage, ElMessageBox... (带样式)
      ElementPlus({
        importStyle: "css",
        useSource: true,
      }),

      Components({
        resolvers: [
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ["ep", "custom"],
          }),
          // 自动导入 Element Plus 组件
          ElementPlusResolver(),
        ],
        dts: path.resolve(pathSrc, "components.d.ts"),
      }),

      Icons({
        compiler: "vue3",
        autoInstall: true,
        // 自定义图标集合
        customCollections: {
          custom: FileSystemIconLoader("src/assets/svg", (svg) =>
            svg.replace(/^<svg /, '<svg fill="currentColor" ')
          ),
        },
      }),

      Inspect(),
    ],

    resolve: {
      alias: {
        //配置项目中引用别名
        "@": pathSrc,
      },
    },

    base: "/vue3-vite-element/", // build打包路径设置

    // 代理配置
    server: {
      host: "0.0.0.0",
      proxy: {
        "/api": {
          target: "http://10.10.11.18:3333/api",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
      hmr: true,
    },

    build: {
      outDir:"dist/vue3-vite-element",
      //vite 在 build 的时候默认会进行压缩计算，但 vite 实际上不提供 gzip 压缩功能，所以不需要让他花时间算压缩后大小，关掉这个设置可以提升打包速度。
      brotliSize: false, //默认为true
    },

    // scss
    cssPreprocessOptions: {
      scss: {
        additionalData: '@import "./src/assets/scss/style.scss";', // 添加公共样式
      },
    },
  };
});
